<?php

/**
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class ModelModel extends CommonModel {

    //array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
    protected $_validate = array(
        array('name', 'require', '模型名称不能为空！'),
        array('tablename', 'require', '表名不能为空！'),
        array('name', '', '该模型名称已经存在！', 0, 'unique', 1),
        array('tablename', '', '该表名已经存在！', 0, 'unique', 1),
    );
    //array(填充字段,填充内容,[填充条件,附加规则])
    protected $_auto = array(
        array("disabled", 0),
        array("type", 0),
        array("sort", 0),
    );

    /**
     * 创建主表和副表
     */
    public function AddModelTable($TableName,$modelid) {
        if(empty($TableName)){
            return false;
        }
        //表前缀
        $TableQianzui = C("DB_PREFIX");
        $Zsql = "CREATE TABLE `".$TableQianzui.$TableName."` ( 
            `id` mediumint(8) unsigned NOT NULL auto_increment,
            `catid` smallint(5) unsigned NOT NULL default '0',
            `typeid` smallint(5) unsigned NOT NULL,
            `title` char(80) NOT NULL default '',
            `style` char(24) NOT NULL default '',
            `thumb` char(100) NOT NULL default '',
            `keywords` char(40) NOT NULL default '', 
            `description` char(255) NOT NULL default '',
            `url` char(100) NOT NULL, 
            `listorder` tinyint(3) unsigned NOT NULL default '0',
            `status` tinyint(2) unsigned NOT NULL default '1', 
            `sysadd` tinyint(1) unsigned NOT NULL default '0',
            `islink` tinyint(1) unsigned NOT NULL default '0',
            `username` char(20) NOT NULL, 
            `inputtime` int(10) unsigned NOT NULL default '0',
            `updatetime` int(10) unsigned NOT NULL default '0',
            PRIMARY KEY  (`id`), KEY `status` (`status`,`listorder`,`id`), KEY `listorder` (`catid`,`status`,`listorder`,`id`), KEY `catid` (`catid`,`status`,`id`)) TYPE=MyISAM;";
        $this->query($Zsql);
        $Fsql = "CREATE TABLE `".$TableQianzui.$TableName."_data` ( 
            `id` mediumint(8) unsigned default '0', 
            `content` text NOT NULL, 
            `paginationtype` tinyint(1) NOT NULL, 
            `maxcharperpage` mediumint(6) NOT NULL,
            `template` varchar(30) NOT NULL, 
            `paytype` tinyint(1) unsigned NOT NULL default '0', 
            `allow_comment` tinyint(1) unsigned NOT NULL default '1', 
            `relation` varchar(255) NOT NULL default '',
            KEY `id` (`id`)) TYPE=MyISAM;";
        $this->query($Fsql);
        $Fidel = array(
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'catid', '栏目', '', '', 1, 6, '/^[0-9]{1,6}$/', '请选择栏目', 'catid', 'a:1:{s:12:\"defaultvalue\";s:0:\"\";}', '', '-99', '-99', 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'typeid', '类别', '', '', 0, 0, '', '', 'typeid', 'a:2:{s:9:\"minnumber\";s:0:\"\";s:12:\"defaultvalue\";s:0:\"\";}', '', '', '', 1, 1, 0, 1, 1, 1, 0, 0, 2, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'title', '标题', '', 'inputtitle', 1, 80, '', '请输入标题', 'title', '', '', '', '', 0, 1, 0, 1, 1, 1, 1, 1, 3, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'keywords', '关键词', '多关键词之间用空格隔开', '', 0, 40, '', '', 'keyword', 'a:2:{s:4:\"size\";s:3:\"100\";s:12:\"defaultvalue\";s:0:\"\";}', '', '-99', '-99', 0, 1, 0, 1, 1, 1, 1, 0, 4, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'description', '摘要', '', '', 0, 255, '', '', 'textarea', 'a:4:{s:5:\"width\";s:2:\"98\";s:6:\"height\";s:2:\"46\";s:12:\"defaultvalue\";s:0:\"\";s:10:\"enablehtml\";s:1:\"0\";}', '', '', '', 0, 1, 0, 1, 0, 1, 1, 1, 5, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'updatetime', '发布时间', '', '', 0, 0, '', '', 'datetime', 'a:3:{s:9:\"fieldtype\";s:3:\"int\";s:6:\"format\";s:11:\"Y-m-d H:i:s\";s:11:\"defaulttype\";s:1:\"0\";}', '', '', '', 0, 1, 0, 0, 0, 0, 0, 0, 10, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'content', '内容', '<div class=\"content_attr\"><label><input name=\"add_introduce\" type=\"checkbox\"  value=\"1\" checked>是否截取内容</label><input type=\"text\" name=\"introcude_length\" value=\"200\" size=\"3\">字符至内容摘要\r\n<label><input type=''checkbox'' name=''auto_thumb'' value=\"1\" checked>是否获取内容第</label><input type=\"text\" name=\"auto_thumb_no\" value=\"1\" size=\"2\" class=\"\">张图片作为标题图片\r\n</div>', '', 1, 999999, '', '内容不能为空', 'editor', 'a:6:{s:7:\"toolbar\";s:4:\"full\";s:12:\"defaultvalue\";s:0:\"\";s:13:\"enablekeylink\";s:1:\"1\";s:10:\"replacenum\";s:1:\"2\";s:9:\"link_mode\";s:1:\"0\";s:15:\"enablesaveimage\";s:1:\"1\";}', '', '', '', 0, 0, 0, 1, 0, 1, 1, 0, 6, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'thumb', '缩略图', '', '', 0, 100, '', '', 'image', 'a:9:{s:4:\"size\";s:2:\"50\";s:12:\"defaultvalue\";s:0:\"\";s:9:\"show_type\";s:1:\"1\";s:14:\"upload_maxsize\";s:4:\"1024\";s:15:\"upload_allowext\";s:20:\"jpg|jpeg|gif|png|bmp\";s:9:\"watermark\";s:1:\"0\";s:13:\"isselectimage\";s:1:\"1\";s:12:\"images_width\";s:0:\"\";s:13:\"images_height\";s:0:\"\";}', '', '', '', 0, 1, 0, 0, 0, 1, 0, 1, 7, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'relation', '相关文章', '', '', 0, 0, '', '', 'omnipotent', 'a:2:{s:8:\"formtext\";s:496:\"<input type=\"hidden\" name=\"info[relation]\" id=\"relation\" value=\"{FIELD_VALUE}\" style=\"50\" ><ul class=\"list-dot\" id=\"relation_text\"></ul><div><input type=\"button\" value=\"添加相关\" onClick=\"omnipotent(\'selectid\',\'?a=public_relationlist&m=Content&g=Contents&modelid={MODELID}\',\'添加相关文章\',1)\" class=\"button\" style=\"width:66px;\"><span class=\"edit_content\"> <input type=\"button\" value=\"显示已有\" onClick=\"show_relation({MODELID},{ID})\" class=\"button\" style=\"width:66px;\"></span></div>\";s:9:\"fieldtype\";s:7:\"varchar\";}', '', '2,6,4,5,1,17,18,7', '', 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'pages', '分页方式', '', '', 0, 0, '', '', 'pages', '', '', '-99', '-99', 0, 0, 0, 1, 0, 0, 0, 0, 9, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'inputtime', '真实发布时间', '', '', 0, 0, '', '', 'datetime', 'a:3:{s:9:\"fieldtype\";s:3:\"int\";s:6:\"format\";s:11:\"Y-m-d H:i:s\";s:11:\"defaulttype\";s:1:\"0\";}', '', '', '', 1, 1, 0, 0, 0, 0, 0, 1, 11, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'url', 'URL', '', '', 0, 100, '', '', 'text', '', '', '', '', 1, 1, 0, 1, 0, 0, 0, 1, 12, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'listorder', '排序', '', '', 0, 6, '', '', 'number', '', '', '', '', 1, 1, 0, 1, 0, 0, 0, 0, 18, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'template', '内容页模板', '', '', 0, 30, '', '', 'template', 'a:2:{s:4:\"size\";s:0:\"\";s:12:\"defaultvalue\";s:0:\"\";}', '', '-99', '-99', 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'allow_comment', '允许评论', '', '', 0, 0, '', '', 'box', 'a:9:{s:7:\"options\";s:33:\"允许评论|1\r\n不允许评论|0\";s:7:\"boxtype\";s:5:\"radio\";s:9:\"fieldtype\";s:7:\"tinyint\";s:9:\"minnumber\";s:1:\"1\";s:5:\"width\";s:2:\"88\";s:4:\"size\";s:0:\"\";s:12:\"defaultvalue\";s:1:\"1\";s:10:\"outputtype\";s:1:\"1\";s:10:\"filtertype\";s:1:\"0\";}', '', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'status', '状态', '', '', 0, 2, '', '', 'box', '', '', '', '', 1, 1, 0, 1, 0, 0, 0, 0, 15, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'username', '用户名', '', '', 0, 20, '', '', 'text', '', '', '', '', 1, 1, 0, 1, 0, 0, 0, 0, 16, 0, 0);",
            "INSERT INTO `".$TableQianzui."model_field` (`modelid`, `field`, `name`, `tips`, `css`, `minlength`, `maxlength`, `pattern`, `errortips`, `formtype`, `setting`, `formattribute`, `unsetgroupids`, `unsetroleids`, `iscore`, `issystem`, `isunique`, `isbase`, `issearch`, `isadd`, `isfulltext`, `isposition`, `listorder`, `disabled`, `isomnipotent`) VALUES($modelid, 'islink', '转向链接', '', '', 0, 0, '', '', 'islink', '', '', '', '', 0, 1, 0, 0, 0, 1, 0, 0, 17, 0, 0);",
        );
        foreach($Fidel as $v){
            $this->query($v);
        }
    }
    
    /**
     * 删除表
     * $table 不带表前缀
     */
    public function DeleteTable($table){
        $status = $this->table_exists($table);
        if($status){
            $this->drop_table($table);
        }
        return true;
    }

    /**
     *生成模型缓存，以模型ID为下标的数组 
     */
    public function ModelCache(){
        $data = $this->select();
        $Cache = array();
        foreach($data as $v){
            $Cache[$v['modelid']] = $v;
        }
        F("Model",$Cache);
        return true;
    }
}

?>
